package com.example.jbmsuppliers.sinopec.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jbmsuppliers.R;
import com.example.jbmsuppliers.sinopec.net.NetWork;
import com.example.jbmsuppliers.sinopec.utlis.ExitAppUtlis;
import com.example.jbmsuppliers.sinopec.view.DialogLoading;
import com.example.jbmsuppliers.sinopec.view.MyToast;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 李征
 * Created by 家帮忙 on 2017/1/11.
 */

public class PhotoLoginActivity extends OvertActivity implements View.OnClickListener {

    @InjectView(R.id.goback_pss)
    TextView gobackPs;
    @InjectView(R.id.weinxin_image)
    ImageView weinxinImage;
    @InjectView(R.id.edit_weinxi_zhs)
    EditText editWeinxiZhs;
    @InjectView(R.id.edit_weinxi_mms)
    EditText editWeinxiMms;
    @InjectView(R.id.layout_jzmm)
    LinearLayout layoutJzmm;
    @InjectView(R.id.layout_shouji_logins)
    Button layoutShoujiLogins;
    @InjectView(R.id.login_regis)
    Button loginRegis;

    private SharedPreferences mSharedPreferences;
    public final static int PHOTO_LOGIN = 0001;
    private DialogLoading mDialog;
    private NetWork netWork;
    private SharedPreferences.Editor editshar;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ExitAppUtlis.getInstance().addActivity(this);
        setContentView(R.layout.photo_login);
        ButterKnife.inject(this);
        mSharedPreferences = getSharedPreferences("sinopecall",
                Activity.MODE_PRIVATE);
        editshar = mSharedPreferences.edit();
        loginRegis.setOnClickListener(this);
        layoutShoujiLogins.setOnClickListener(this);
        gobackPs.setOnClickListener(this);
        netWork = new NetWork(handler);
        mDialog = new DialogLoading(this, "登录中...", true);
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case PHOTO_LOGIN: {
                    String restlt = (String) msg.obj;
                    if (restlt != null) {
                        if (restlt.equals("Login OK")) {
                            if (mDialog != null) {
                                mDialog.dismiss();
                            }
                            editshar.putString("phname", editWeinxiZhs.getText().toString()).commit();
                            editshar.putString("phpwd", editWeinxiMms.getText().toString()).commit();
                            editshar.putString("weinxinlogin","shouji");
                            editshar.putBoolean("IsLogin", true).commit();
                            sendBroadcast(new Intent("isBack").putExtra("login",true).putExtra("relogin","login1"));//
                            finish();
                            MainActivity.goNewHome();

                        } else if (restlt.equals("FAILED")) {
                            if (mDialog != null) {
                                mDialog.dismiss();
                            }
                            hint("登录失败请检查网络");
                        } else {
                            if (mDialog != null) {
                                mDialog.dismiss();
                            }
                            hint("用户名或密码错误");
                        }
                    }
                    break;
                }

            }


        }
    };

    @Override
    public void onBackPressed() {
        exit("已注销用户是否退出？");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.goback_pss:
                if (mSharedPreferences.getBoolean("IsLogin",false)!=true){
                finish();
                    MainActivity.goNewHome();
                }else {
                finish();}
                break;
            case R.id.layout_shouji_logins:
                //登录
                if (editWeinxiZhs.getText().toString().equals("")) {
                    MyToast.show(PhotoLoginActivity.this, "手机号不能为空！", Toast.LENGTH_SHORT);
                } else if (editWeinxiZhs.getText().toString().length() > 11 || editWeinxiZhs.getText().toString().length() < 11) {
                    MyToast.show(PhotoLoginActivity.this, "请检查手机号长度！", Toast.LENGTH_SHORT);
                } else if (editWeinxiMms.getText().toString().equals("")) {
                    MyToast.show(PhotoLoginActivity.this, "密码不能为空！", Toast.LENGTH_SHORT);
                } else {
                    netWork.getPhotoLogin(editWeinxiZhs.getText().toString(), editWeinxiMms.getText().toString());
                    mDialog.show();
                }
                break;
            case R.id.login_regis:
                //快速注册
                this.startActivity(new Intent(this, RegisterPhotoActivity.class));
                finish();
                break;

        }
    }


}
